package com.example.user.customviewdemo.popupwindow;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.example.user.customviewdemo.R;

import butterknife.Bind;
import butterknife.ButterKnife;

public class PopupWindowActivity extends AppCompatActivity implements View.OnClickListener {

    @Bind(R.id.button)
    Button showButton;
    @Bind(R.id.button1)
    Button dismissButton;
    @Bind(R.id.button2)
    Button dialogButton;

    private LayoutInflater inflater;
    private View rootView;
    private PopupWindow popupWindow;
    private PopupWindow dialogPopuWindow;
    private View dialogContentView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inflater = LayoutInflater.from(this);
        rootView = inflater.inflate(R.layout.activity_popup_window, null);
        setContentView(rootView);
        ButterKnife.bind(this);


        /**
         *  创建PopupWindow(创建popupwindow必须要有contentView、width、height)
         *  方法一：
         *  public PopupWindow (Context context)
         *  popWnd.setContentView(contentView);
         *  popWnd.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
         *  popWnd.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
         *  方法二：
         *  public PopupWindow(View contentView)
         *  方法三：
         *  public PopupWindow(View contentView, int width, int height)
         *  方法四：
         *  public PopupWindow(View contentView, int width, int height, boolean focusable)
         */
        View contentView = inflater.inflate(R.layout.popup_window_layout, null);
        ListView listView = (ListView) contentView.findViewById(R.id.listview);
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.popup_window_list)));
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        contentView.setOnClickListener(this);

        /**
         * 使用popuwindow模仿的dialog
         */
        dialogContentView = inflater.inflate(R.layout.dialog_popu_window_layout,null);
        dialogPopuWindow = new PopupWindow(this);
        dialogPopuWindow.setContentView(dialogContentView);
        dialogPopuWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        dialogPopuWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        dialogContentView.setOnClickListener(this);

        /**
         * 设置按钮点击事件
         */
        showButton.setOnClickListener(this);
        dismissButton.setOnClickListener(this);
        dialogButton.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        if (v == showButton) {
            popupWindow.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
            // popupWindow.showAsDropDown(dismissButton, 0, 0);
        } else if (v == dismissButton) {
            popupWindow.dismiss();
        }else if(v == dialogButton){
            dialogPopuWindow.showAtLocation(rootView,Gravity.BOTTOM,0,0);

        }else if(v == dialogContentView){
            dialogPopuWindow.dismiss();
        }

    }


}
